/**
 * file:GridBuildingInfoHandler
 */
package com.hongguan.oms.web.grid;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.common.ConstGrid;
import com.hongguan.oms.domain.grid.GridGroupCustInfo;
import com.hongguan.oms.service.gis.GisService;
import com.hongguan.oms.service.grid.GridAddressInfoService;
import com.hongguan.oms.service.grid.GridAreasService;
import com.hongguan.oms.service.grid.GridCommunityInfoService;
import com.hongguan.oms.service.grid.GridGroupCustInfoService;
import com.hongguan.oms.service.grid.GridSchoolInfoService;
import com.hongguan.oms.service.grid.GridUnitService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;

/**
 * @author ���г�
 * @version $Id: GridSchoolInfoHandler.java 130 $
 */
public class GridUnitInfoHandler extends BaseHandler {

	private GridSchoolInfoService gridSchoolInfoService;
	private GridUnitService gridUnitService;
	private GridAreasService gridAreasService;
	private GridCommunityInfoService gridCommunityInfoService; 
	private GisService gisService;
	private GridAddressInfoService gridAddressInfoService;
	private GridGroupCustInfoService gridGroupCustInfoService;

	/**
	 * ��ѯ���е�Ӫ��Ԫ�б�
	 * @param request
	 * @param response
	 * @return mv
	 * @throws Exception
	 */
	public ModelAndView unitList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = this.getSuccessModelView();
		Map<String, Object> param = new HashMap<String, Object>();
		String name = request.getParameter("unitName");
		String address = request.getParameter("unitAddress");
		if(name!=null&!"".equals(name)){
			param.put("unitName", name);
		}
		if(address!=null && !"".equals(address)){
			param.put("unitAddress", address);
		}
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		start = start == null ? "0" : start;
		limit = limit == null ? "15" : limit;
		int beginRow = Integer.parseInt(start);
		int endRow = Integer.parseInt(limit) + beginRow;
		param.put("beginRow", beginRow);
		param.put("endRow", endRow);
		mv.addObject("result",gridUnitService.searchGridUnit(param));
		mv.addObject("total", gridUnitService.searchGridUnitCount(param));
		return mv;
	}

	/**
	 * getSchoolInfo
	 * @param request
	 * @return
	 */
	public Map getPrepareInfo(HttpServletRequest request) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("school_id", Util.toNull(request.getParameter("school_id")));
		param.put("grid_id", Util.toNull(request.getParameter("grid_id")));
		param.put("unit_id", Util.toNull(request.getParameter("unit_id")));
		param.put("school_name", Util.toNull(request.getParameter("school_name")));
		param.put("school_type", Util.toNull(request.getParameter("school_type")));
		param.put("teachers_num", Util.toNull(request.getParameter("teachers_num")));
		param.put("students_num", Util.toNull(request.getParameter("students_num")));
		param.put("contact_id", Util.toNull(request.getParameter("contact_id")));
		param.put("contact_name", Util.toNull(request.getParameter("contact_name")));
		param.put("phone_num_office", Util.toNull(request.getParameter("contact_phone")));
		param.put("school_char", Util.toNull(request.getParameter("school_char")));
		
		param.put("unit_id", Util.toNull(request.getParameter("unit_id")));
		param.put("grid_id", Util.toNull(request.getParameter("grid_id")));
		param.put("unit_type", ConstGrid.UNIT_TYPE_SCHOOL);
		param.put("entity_type", ConstGrid.UNIT_TYPE_SCHOOL);
		param.put("unit_name", Util.toNull(request.getParameter("school_name")));
		
		param.put("unit_manager", Util.toNull(request.getParameter("unit_manager_user_id")));
		param.put("user_id", Util.toNull(request.getParameter("unit_manager_user_id")));//��tap_c_user_info�в���
		param.put("user_name", Util.toNull(request.getParameter("unit_manager_user_name")));
		param.put("user_phone", Util.toNull(request.getParameter("unit_manager_user_phone")));
		
		param.put("address_id", Util.toNull(request.getParameter("address_id")));
		String district=request.getParameter("district");
		String neighbourhood=request.getParameter("neighbourhood");
		String street=request.getParameter("street");
		String alley=request.getParameter("alley");
		String door_num=request.getParameter("door_num");
		
		String address_detail= gridCommunityInfoService.getAddressDetail(district, neighbourhood, street, alley, door_num);
		param.put("address_detail", address_detail);
		
		param.put("district", Util.toNull(district));
		param.put("neighbourhood", Util.toNull(neighbourhood));
		param.put("street", Util.toNull(street));
		param.put("alley", Util.toNull(alley));
		param.put("door_num", Util.toNull(door_num));
		
		param.put("gis_id", Util.toNull(request.getParameter("gis_id")));//gis_info��
		param.put("icon_id", Util.toNull(request.getParameter("icon_id")));
		param.put("type", ConstGrid.GIS_TYPE_AREA);
		String coords_info=request.getParameter("coords_info");
		if(StringUtils.isNotBlank(coords_info)){
			Map map=gridCommunityInfoService.setNamePosAndRectScale(coords_info);
			String namepos=map.get("namepos").toString();
			String rectScale=map.get("rectScale").toString();
			param.put("name_pos", namepos);
			param.put("rect_scale", rectScale);
		}
		param.put("coords_info", Util.toNull(coords_info));
		param.put("line_color", Util.toNull(request.getParameter("line_color")));
		param.put("main_fill_color", Util.toNull(request.getParameter("main_fill_color")));
		return param;
	}


	public void setGridUnitService(GridUnitService gridUnitService) {
		this.gridUnitService = gridUnitService;
	}
	public void setGridSchoolInfoService(GridSchoolInfoService gridSchoolInfoService) {
		this.gridSchoolInfoService = gridSchoolInfoService;
	}

	public void setGisService(GisService gisService) {
		this.gisService = gisService;
	}

	public void setGridAddressInfoService(
			GridAddressInfoService gridAddressInfoService) {
		this.gridAddressInfoService = gridAddressInfoService;
	}


	public void setGridAreasService(GridAreasService gridAreaService) {
		this.gridAreasService = gridAreaService;
	}
	public void setGridCommunityInfoService(
			GridCommunityInfoService gridCommunityInfoService) {
		this.gridCommunityInfoService = gridCommunityInfoService;
	}
	public void setGridGroupCustInfoService(
			GridGroupCustInfoService gridGroupCustInfoService) {
		this.gridGroupCustInfoService = gridGroupCustInfoService;
	}
}
